package com.hilihili.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户等级
 * @Author xhc
 * @Date 2023/12/17 15:24
 * @Version 1.0
 */
@RestController
@RequestMapping("/hilihili/experience")
public class userEXPController extends BaseController {
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 修改用户等级和经验值
     * @param userId 用户id
     * @param exp 经验值增量
     * @return
     */
    @GetMapping("/addUserExperience")
    public AjaxResult addUserExperience(Long userId,Long exp){
        if (userId == null) return AjaxResult.error("用户id不能为空");
        SysUser sysUser = iSysUserService.selectUserById(userId);
        // 当前等级
        int levelOld = validateLevel(sysUser.getExperience());
        int levelNow = validateLevel(sysUser.getExperience() + exp);
        if (levelNow == levelOld){
            // 改变经验值
            iSysUserService.updateUserExperience(userId, exp);
            return AjaxResult.success();
        }else {
            // 改变经验值，并改变等级
            iSysUserService.updateUserExperience(userId, exp);
            iSysUserService.updateUserLevel(userId, levelNow);
            return AjaxResult.success();
        }
    }

    /***
     * 判断用户等级
     * @param exp
     * @return
     */
    public int validateLevel(Long exp){
        if(exp > 28800){
            return 6;
        }else if(exp > 10800){
            return 5;
        }else if(exp > 4500){
            return 4;
        }else if(exp > 1500){
            return 3;
        }else if(exp > 200){
            return 2;
        }else if (exp > 50){
            return 1;
        }else {
            return 0;
        }
    }
}
